package cn.fy.sqlite.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

import cn.fy.sqlite.R;
import cn.fy.sqlite.adapter.UserAdapter;
import cn.fy.sqlite.dao.UserDao;
import cn.fy.sqlite.databinding.ActivitySqliteBinding;
import cn.fy.sqlite.databinding.ActivityThreadBinding;
import cn.fy.sqlite.model.User;

public class SqliteActivity extends AppCompatActivity {

    private ActivitySqliteBinding binding;

    private UserDao userDao;

    ArrayList<User> userList;
    UserAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_sqlite);
        binding = ActivitySqliteBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Sqlite");

        userDao = new UserDao(SqliteActivity.this);

        userList = userDao.queryUser(new User());
        adapter = new UserAdapter(SqliteActivity.this, userList);
        binding.listView.setAdapter(adapter);

        binding.btnAddUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAddUserDialog();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showAddUserDialog() {
        AlertDialog.Builder customizeDialog = new AlertDialog.Builder(SqliteActivity.this);
        final View dialogView = LayoutInflater.from(SqliteActivity.this).inflate(R.layout.layout_user_dialog, null);

        customizeDialog.setTitle("新增用户");
        customizeDialog.setView(dialogView);
        customizeDialog.setNegativeButton("取消", null);
        customizeDialog.setPositiveButton("确定",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 获取EditView中的输入内容
                EditText name = (EditText) dialogView.findViewById(R.id.txt_dialog_name);
                EditText age = (EditText) dialogView.findViewById(R.id.txt_dialog_age);

                User user = new User();
                user.setName(name.getText().toString());
                user.setAge(Integer.parseInt(age.getText().toString()));

                userDao.addUser(user);

                refreshListView();

                Toast.makeText(SqliteActivity.this, "新增用户成功", Toast.LENGTH_SHORT).show();
            }
        });
        customizeDialog.show();
    }

    public void refreshListView() {
        userList = userDao.queryUser(new User());
        adapter = new UserAdapter(SqliteActivity.this, userList);
        binding.listView.setAdapter(adapter);
    }
}